package com.tuanzi.loan.business.repository.settlement.impl;

import static com.tuanzi.loan.generated.jooq.loan.Tables.LOAN_COMPANY_INFO;
import static com.tuanzi.loan.generated.jooq.loan.Tables.LOAN_FIRSTPARTY_DATA_CHANGE_RECORD;

import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.tuanzi.loan.business.repository.settlement.custom.FirstpartyDataChangeRecordRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.settlement.FirstpartyDataChangeRecordRequest;
import com.tuanzi.loan.vo.response.settlement.FirstpartyDataChangeRecordResponse;

/**
 * @author caiwanlin
 * @create 2018-01-18 10:12
 */
public class FirstpartyDataChangeRecordRepositoryImpl extends JOOQBaseRepository implements FirstpartyDataChangeRecordRepositoryCustom {
    @Override
    protected TableImpl getTable() {
        return LOAN_FIRSTPARTY_DATA_CHANGE_RECORD;
    }

    @Override
    public Page<FirstpartyDataChangeRecordResponse> list(FirstpartyDataChangeRecordRequest searchRequest, Pageable pageable) {
        fields(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.ID,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.COMPANY_ID,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.JIAFANG_ID,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.COMPANY_NAME,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.PRODUCT_NAME,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.COP_MODE,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.FIRSTPARTY_CREATE_TIME,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.FIRSTPARTY_CREATOR,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.UPDATE_TIME,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.UPDATOR,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.SETTLEMENT_TYPE,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.SETTLEMENT_PERCENT_PRICE,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.SETTLEMENT_YUAN_PRICE,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.SETTLEMENT_START_TIME,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.SETTLEMENT_END_TIME,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.OLD_SPREAD_CHANNEL,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.NEW_SPREAD_CHANNEL,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.OLD_CLICK_COUNT,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.NEW_CLICK_COUNT,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.OLD_LOAN_AMOUNT,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.NEW_LOAN_AMOUNT,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.OLD_REGISTER_COUNT,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.NEW_REGISTER_COUNT,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.OLD_SETTLEMENT_AMOUNT,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.NEW_SETTLEMENT_AMOUNT,
                LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.REMARK
                );
//            leftJoin(LOAN_COMPANY_INFO,LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.COMPANY_ID.eq(LOAN_COMPANY_INFO.ID));
            between(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.UPDATE_TIME,searchRequest.getStartTime(),searchRequest.getEndTime());
            contains(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.COMPANY_NAME,searchRequest.getCompanyName());
//            contains(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.PRODUCT_NAME,searchRequest.getProductName());
        return search(pageable,new FirstpartyDataChangeRecordResponseRecordMapper());
    }


    private final class FirstpartyDataChangeRecordResponseRecordMapper implements RecordMapper<Record, FirstpartyDataChangeRecordResponse> {

        @Override
        public FirstpartyDataChangeRecordResponse map(Record record) {
            FirstpartyDataChangeRecordResponse response = new FirstpartyDataChangeRecordResponse();
            response.setId(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.ID));
            response.setCompanyId(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.COMPANY_ID));
            response.setCompanyName(record.get(LOAN_COMPANY_INFO.COMPANY_NAME));
            response.setProductName(record.get(LOAN_COMPANY_INFO.PRODUCT_NAME));
            response.setCopMode(record.get(LOAN_COMPANY_INFO.COP_MODE));
            response.setFirstpartyCreator(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.FIRSTPARTY_CREATOR));
            response.setFirstpartyCreateTime(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.FIRSTPARTY_CREATE_TIME));
            response.setJiafangId(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.JIAFANG_ID));
            response.setOldClickCount(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.OLD_CLICK_COUNT));
            response.setNewClickCount(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.NEW_CLICK_COUNT));
            response.setOldLoanAmount(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.OLD_LOAN_AMOUNT));
            response.setNewLoanAmount(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.NEW_LOAN_AMOUNT));
            response.setOldRegisterCount(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.OLD_REGISTER_COUNT));
            response.setNewRegisterCount(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.NEW_REGISTER_COUNT));
            response.setOldSpreadChannel(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.OLD_SPREAD_CHANNEL));
            response.setNewSpreadChannel(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.NEW_SPREAD_CHANNEL));
            response.setSettlementType(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.SETTLEMENT_TYPE));
            response.setSettlementPercentPrice(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.SETTLEMENT_PERCENT_PRICE));
            response.setSettlementYuanPrice(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.SETTLEMENT_YUAN_PRICE));
            response.setOldSettlementAmount(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.OLD_SETTLEMENT_AMOUNT));
            response.setNewSettlementAmount(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.NEW_SETTLEMENT_AMOUNT));
            response.setSettlementStartTime(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.SETTLEMENT_START_TIME));
            response.setSettlementEndTime(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.SETTLEMENT_END_TIME));
            response.setRemark(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.REMARK));
            response.setUpdateTime(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.UPDATE_TIME));
            response.setUpdator(record.get(LOAN_FIRSTPARTY_DATA_CHANGE_RECORD.UPDATOR));
            return response;
        }
    }
}
